//
//  NeedHelpsJSONResponse.m
//

#import "NeedHelpsJSONResponse.h"
#import "Need.h"
#import "Help.h"
#import "JSON/JSON.h"

@implementation NeedHelpsJSONResponse

- (NSError*)request:(TTURLRequest*)request processResponse:(NSHTTPURLResponse*)response data:(id)data
{

    NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    // Parse the JSON data that we retrieved from the server.
    NSDictionary *json = [responseBody JSONValue];
    [responseBody release];
    
    // Drill down into the JSON object to get the parts
    // that we're actually interested in.
    NSDictionary *root = [json objectForKey:@"rsp"];
  //  totalObjectsAvailableOnServer = [[root objectForKey:@"total"] integerValue];

    // Now wrap the results from the server into a domain-specific object.
    NSDictionary *rawResult = [root objectForKey:@"need"];
	
	Need *result = [[[Need alloc] init] autorelease];
	result._id = [[rawResult objectForKey:@"Need"] objectForKey:@"id"];
	result.subject = [[rawResult objectForKey:@"Need"] objectForKey:@"subject"];
	result.text = [[rawResult objectForKey:@"Need"] objectForKey:@"text"];
	result.created = [[rawResult objectForKey:@"Need"] objectForKey:@"created"];
	result.timediff = [[rawResult objectForKey:@"Need"] objectForKey:@"timediff"];
	result.city = [[rawResult objectForKey:@"Need"] objectForKey:@"city"];
	result.country = [[rawResult objectForKey:@"Need"] objectForKey:@"country"];
	result.status = [[rawResult objectForKey:@"Need"] objectForKey:@"status"];
	result.lat = [[rawResult objectForKey:@"Need"] objectForKey:@"lat"];
	result.lng = [[rawResult objectForKey:@"Need"] objectForKey:@"lng"];
	result.helps = [[rawResult objectForKey:@"Need"] objectForKey:@"helps"];
	
	User *user = [[[User alloc] init] autorelease];
	user._id = [[rawResult objectForKey:@"User"] objectForKey:@"id"];
	user.name = [[rawResult objectForKey:@"User"] objectForKey:@"name"];
	user.lastname = [[rawResult objectForKey:@"User"] objectForKey:@"lastname"];
	user.nickname = [[rawResult objectForKey:@"User"] objectForKey:@"nickname"];
	user.displayname = [[rawResult objectForKey:@"User"] objectForKey:@"displayname"];
	user.biography = [[rawResult objectForKey:@"User"] objectForKey:@"biography"];
	user.pictureurl = [[rawResult objectForKey:@"User"] objectForKey:@"pictureurl"];
	user.lat = [[rawResult objectForKey:@"User"] objectForKey:@"lat"];
	user.lng = [[rawResult objectForKey:@"User"] objectForKey:@"lng"];
	user.whyhelp = [[rawResult objectForKey:@"User"] objectForKey:@"whyhelp"];
	
	result.user = user;
	
	NSArray *helpsResults = [rawResult objectForKey:@"Helps"];
	NSMutableArray *helps = [[NSMutableArray alloc] init];
	
	for (NSDictionary *rawHelps in helpsResults) {
		
		Help *help = [[[Help alloc] init] autorelease];
		help._id = [[rawHelps objectForKey:@"Help"] objectForKey:@"id"];
		help.text = [[rawHelps objectForKey:@"Help"] objectForKey:@"text"];
		help.stars = [[rawHelps objectForKey:@"Help"] objectForKey:@"stars"];
		help.created = [[rawHelps objectForKey:@"Help"] objectForKey:@"created"];
		help.timediff = [[rawHelps objectForKey:@"Help"] objectForKey:@"timediff"];
		
		NSDictionary *rawHelpsUser = [rawHelps objectForKey:@"Help"];
		User *user = [[[User alloc] init] autorelease];
		user._id = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"id"];
		user.name = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"name"];
		user.lastname = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"lastname"];
		user.nickname = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"nickname"];
		user.displayname = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"displayname"];
		user.biography = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"biography"];
		user.pictureurl = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"pictureurl"];
		user.lat = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"lat"];
		user.lng = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"lng"];
		user.whyhelp = [[rawHelpsUser objectForKey:@"User"] objectForKey:@"whyhelp"];
		
		help.user = user;
		
	//	TTLOG(@" we Added a help: %@", help._id);
		[helps addObject:help];
	
    }
	
	result.allHelps = helps;
	
	[self.objects addObject:result];
    
    return nil;
}


- (NSString *)format { return @"json"; }

@end
